package com.xfly.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.utils.GlideUtil;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.TitleBarView;
import com.xfly.app.bean.BankBean;
import com.xfly.app.bean.UserBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TxActivity extends BaseMvpActivity
{

    TitleBarView titleBarView;

    LinearLayout yhk;

    ImageView img;

    TextView kh,bankname,ktxnum,all,tx;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();

    EditText num;

    String id = "";
    UserBean datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_tx);
    }

    @Override
    public void initViews() {
        titleBarView = findViewById(R.id.title_bar);
        yhk = findViewById(R.id.yhk);
        kh = findViewById(R.id.kh);
        bankname = findViewById(R.id.bankname);
        img = findViewById(R.id.img);
        ktxnum = findViewById(R.id.ktxnum);
        all = findViewById(R.id.all);
        num = findViewById(R.id.num);
        tx = findViewById(R.id.tx);


        titleBarView.setText("提现");
    }

    @Override
    public void initEvents() {
        titleBarView.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                num.setText(datas.getMoney());
            }
        });

        tx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String,String> map = new HashMap<>();
                map.put("bank_id",id);
                map.put("money",num.getText().toString());
                apiDataRepository.tx(map, new ApiNetResponse<Object>() {
                    @Override
                    public void onSuccess(Object data) {
                        showMsg("提交成功");
                        finish();
                    }
                });
            }
        });

        yhk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(),BankListActivity.class);
                intent.putExtra("select",true);
                startActivityForResult(intent,2);
            }
        });

        getdata();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserinfo();
    }

    private void getdata(){
        Map<String,String> map = new HashMap<>();
        map.put("page","1");
        apiDataRepository.banklist(map, new ApiNetResponse<List<BankBean>>() {
            @Override
            public void onSuccess(List<BankBean> data) {

                GlideUtil.showImg(getContext(),data.get(0).bank_logo+"",img);
                bankname.setText(data.get(0).bank);
                kh.setText(data.get(0).bank_card);
                id = data.get(0).id;


            }

            @Override
            public void onFaile(String info, List<BankBean> data, Throwable throwable) {
                super.onFaile(info, data, throwable);

            }
        });
    }

    private void getUserinfo(){
        apiDataRepository.myInfo(null, new ApiNetResponse<UserBean>() {
            @Override
            public void onSuccess(UserBean data) {
                datas = data;
                ktxnum.setText("可提现金额￥"+data.getMoney());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 2){
            if(data!=null){
                id = data.getStringExtra("id");
                GlideUtil.showImg(getContext(),data.getStringExtra("logo"),img);
                bankname.setText(data.getStringExtra("name"));
                kh.setText(data.getStringExtra("num"));
            }
        }
    }
}
